<?php
/*
 * Created on 21 mars 2009
 * Created by Ludovic Reenaers
 * lreenaers@hotmail.com
 */
 //require_once("beans/User.php");
 
 class UserDAO extends DAO{
 	private $Bean;
 	
 	function add($bean){
 		//return null si user déjà existant
 		$this->Bean = null;
 		$query="select * from user where email='$bean->getEmail()';";
		$statement=$this->pdo->query($query);
		$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
		if( empty($array_obj)){
			
			$stamp = time();
			$query="insert into user (id,nom,prenom,pseudo,email,rue,numero,boite,cp,ville,pays,langue,ip,ddn,pws_hash,profile,created,last_connected)";
			$query.=" values (null,'".$bean->getNom()."','".$bean->getPrenom()."','".$bean->getPseudo()."','".$bean->getEmail()."','".$bean->getRue()."','".$bean->getNumero()."','".$bean->getBoite()."','".$bean->getCp()."','".$bean->getVille()."','".$bean->getPays()."',".$bean->getLangue()->getId().",'".$bean->getIp()."',DATE('".$bean->getDdn()."'),'".$bean->getPwsHash()."',".$bean->getProfile()->getId().",NOW(),NOW());";
			try{
				$this->pdo->exec($query);
				$this->Bean = $this->getById((int)$this->pdo->lastInsertId());
			}catch(Exception $e){
				echo "<br/>QUERY:".$query."<br/>";
				echo $e->getMessage();
			}
		}
		return $this->Bean;
 	}
 	function getByEmail($email){
		//return a Profile with a given id
		try
		{
				$query="select * from user where email='$email';";
				$statement=$this->pdo->query($query);
				$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
				foreach($array_obj as $Bean){
					$this->Bean = $this->getById((int)$Bean->id);
				}
		}
		catch(Exception $e)	{
			echo "<br/>QUERY:".$query."<br/>";
			throw $e;
		}
		
		return $this->Bean;
	}
 	function getById($id){
		//return a Profile with a given id
		try
		{
				$query="select * from user where id=$id;";
				$statement=$this->pdo->query($query);
				$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
				foreach($array_obj as $Bean){
					$this->Bean = new User((int)$Bean->id);
					$this->Bean->setNom($Bean->nom);
					$this->Bean->setPrenom($Bean->prenom);
					$this->Bean->setPseudo($Bean->pseudo);
					$this->Bean->setEmail($Bean->email);
					$this->Bean->setRue($Bean->rue);
					$this->Bean->setNumero($Bean->numero);
					$this->Bean->setBoite($Bean->boite);
					$this->Bean->setCp($Bean->cp);
					$this->Bean->setVille($Bean->ville);
					$this->Bean->setPays($Bean->pays);
					$this->Bean->setLangue($this->factory->getLangueDAO()->getById((int)$Bean->langue));
					$this->Bean->setIp($Bean->ip);
					list($year,$month,$day)=split('[/,-]',$Bean->ddn);
					$this->Bean->setDdn($day,$month,$year);
					$this->Bean->setCreated($Bean->created);
					$this->Bean->setLastConnected($Bean->last_connected);
					$this->Bean->setPwsHash($Bean->pws_hash,$Bean->pws_hash);
					$this->Bean->setProfile($this->factory->getProfileDAO()->getById((int)$Bean->profile));
					$this->Bean->setActive($Bean->active);
				}
		}
		catch(Exception $e)	{
			throw $e;		
		}
		
		return $this->Bean;
	}
	function getAnonymousUser(){
		//return a Profile with a given id
		try
		{
				$query="select * from user where id=(select id from user where profile=(select id from user_profile where lower(name)='anonymous'));";
				$statement=$this->pdo->query($query);
				$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
				foreach($array_obj as $Bean){
					$this->Bean = $this->getById((int)$Bean->id);
				}
		}
		catch(Exception $e)	{
			throw $e;		
		}
		
		return $this->Bean;
	}
 }
?>
